package com.jdone.compus.repository;

import com.jdone.compus.model.BlowDevice;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface BlowDeviceRepository extends JpaRepository<BlowDevice, Long> {

    Page<BlowDevice> findAll(Specification<BlowDevice> spec, Pageable pageable);

    long countByCreatedAtBetween(LocalDateTime start, LocalDateTime end);

    long countByCreatedAtAfter(LocalDateTime start);

    long countByCreatedAtBefore(LocalDateTime end);

    Optional<BlowDevice> findByIdAndIsDeleted(Long id, Integer isDeleted);

    List<BlowDevice> findByIdInAndIsDeleted(List<Long> ids, Integer isDeleted);

    // 批量更新 useTime
    @Modifying
    @Query("UPDATE BlowDevice b SET b.useTime = :useTime WHERE b.id IN :ids")
    int updateUseTimeByIds(@Param("useTime") String useTime, @Param("ids") List<Long> ids);
}
